{{- define "operator_trivy_operator_resources" }}
cpu: 50m
memory: 50Mi
{{- end }}

{{- if (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: operator
  namespace: d8-{{ .Chart.Name }}
  {{- include "helm_lib_module_labels" (list . (dict "app" "operator")) | nindent 2 }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: operator
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: operator
      minAllowed:
        {{- include "operator_trivy_operator_resources" . | nindent 8 }}
      maxAllowed:
        cpu: 100m
        memory: 100Mi
    {{- include "helm_lib_vpa_kube_rbac_proxy_resources" . | nindent 4 }}
{{- end }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: operator
  namespace: d8-{{ .Chart.Name }}
  {{- include "helm_lib_module_labels" (list . (dict "app" "operator")) | nindent 2 }}
spec:
  replicas: 1
  revisionHistoryLimit: 2
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: operator
  template:
    metadata:
      labels:
        app: operator
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
    spec:
      imagePullSecrets:
      - name: deckhouse-registry
      serviceAccountName: operator-trivy
      automountServiceAccountToken: true
      {{- include "helm_lib_node_selector" (tuple . "system") | nindent 6 }}
      {{- include "helm_lib_tolerations" (tuple . "system") | nindent 6 }}
      {{- include "helm_lib_priority_class" (tuple . "cluster-low") | nindent 6 }}
      {{- include "helm_lib_module_pod_security_context_run_as_user_deckhouse" . | nindent 6 }}
      containers:
      - name: "operator"
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem_capabilities_drop_all" . | nindent 8 }}
        image: {{ include "helm_lib_module_image" (list . "operator") }}
        env:
          {{- include "helm_lib_envs_for_proxy" . | nindent 10 }}
          - name: OPERATOR_NAMESPACE
            value: d8-operator-trivy
          {{- $enabledNamespaces := .Values.operatorTrivy | dig "internal" "enabledNamespaces" (list) }}
          - name: OPERATOR_TARGET_NAMESPACES
            value: {{ gt (len $enabledNamespaces) 0 | ternary $enabledNamespaces (list "default") | join "," | quote }}
          - name: OPERATOR_EXCLUDE_NAMESPACES
            value: ""
          - name: OPERATOR_TARGET_WORKLOADS
            value: "pod,replicaset,replicationcontroller,statefulset,daemonset,cronjob,job"
          - name: OPERATOR_SERVICE_ACCOUNT
            value: "operator-trivy"
          - name: OPERATOR_LOG_DEV_MODE
            value: "false"
          - name: OPERATOR_SCAN_JOB_TIMEOUT
            value: "5m"
          {{- if $.Values.global.modulesImages.registry.CA }}
          - name: OPERATOR_SCAN_JOB_REGISTRY_CA
            value: | {{ $.Values.global.modulesImages.registry.CA | nindent 14 }}
          {{- end }}
          - name: OPERATOR_CONCURRENT_SCAN_JOBS_LIMIT
            value: "10"
          - name: OPERATOR_SCAN_JOB_RETRY_AFTER
            value: "30s"
          - name: OPERATOR_BATCH_DELETE_LIMIT
            value: "10"
          - name: OPERATOR_BATCH_DELETE_DELAY
            value: "10s"
          - name: OPERATOR_METRICS_BIND_ADDRESS
            value: "127.0.0.1:8080"
          - name: OPERATOR_METRICS_FINDINGS_ENABLED
            value: "true"
          - name: OPERATOR_METRICS_VULN_ID_ENABLED
            value: "true"
          - name: OPERATOR_HEALTH_PROBE_BIND_ADDRESS
            value: ":9090"
          - name: OPERATOR_VULNERABILITY_SCANNER_ENABLED
            value: "true"
          - name: OPERATOR_VULNERABILITY_SCANNER_SCAN_ONLY_CURRENT_REVISIONS
            value: "true"
          - name: OPERATOR_SCANNER_REPORT_TTL
            value: "24h"
          - name: OPERATOR_CLUSTER_COMPLIANCE_ENABLED
            value: "true"
          - name: OPERATOR_CONFIG_AUDIT_SCANNER_ENABLED
            value: "true"
          - name: OPERATOR_RBAC_ASSESSMENT_SCANNER_ENABLED
            value: "true"
          - name: OPERATOR_INFRA_ASSESSMENT_SCANNER_ENABLED
            value: "true"
          - name: OPERATOR_CONFIG_AUDIT_SCANNER_SCAN_ONLY_CURRENT_REVISIONS
            value: "true"
          - name: OPERATOR_EXPOSED_SECRET_SCANNER_ENABLED
            value: "true"
          - name: OPERATOR_WEBHOOK_BROADCAST_URL
            value: ""
          - name: OPERATOR_WEBHOOK_BROADCAST_TIMEOUT
            value: "30s"
          - name: OPERATOR_PRIVATE_REGISTRY_SCAN_SECRETS_NAMES
            value: "{}"
          - name: OPERATOR_ACCESS_GLOBAL_SECRETS_SERVICE_ACCOUNTS
            value: "true"
          - name: OPERATOR_BUILT_IN_TRIVY_SERVER
            value: "true"
          - name: TRIVY_SERVER_HEALTH_CHECK_CACHE_EXPIRATION
            value: "10h"
          - name: OPERATOR_MERGE_RBAC_FINDING_WITH_CONFIG_AUDIT
            value: "false"
        ports:
          - name: metrics
            containerPort: 8080
          - name: probes
            containerPort: 9090
        readinessProbe:
          httpGet:
            path: /readyz/
            port: 9090
          initialDelaySeconds: 5
          periodSeconds: 10
          successThreshold: 1
          failureThreshold: 3
        livenessProbe:
          httpGet:
            path: /healthz/
            port: 9090
          initialDelaySeconds: 5
          periodSeconds: 10
          successThreshold: 1
          failureThreshold: 10
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_only_logs" . | nindent 12 }}
{{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "operator_trivy_operator_resources" . | nindent 12 }}
{{- end }}
      - name: kube-rbac-proxy
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem" . | nindent 8 }}
        image: {{ include "helm_lib_module_common_image" (list . "kubeRbacProxy") }}
        args:
        - "--secure-listen-address=$(KUBE_RBAC_PROXY_LISTEN_ADDRESS):8081"
        - "--v=2"
        - "--logtostderr=true"
        - "--stale-cache-interval=1h30m"
        ports:
        - containerPort: 8081
          name: https-metrics
        env:
        - name: KUBE_RBAC_PROXY_LISTEN_ADDRESS
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: KUBE_RBAC_PROXY_CONFIG
          value: |
            upstreams:
            - upstream: http://127.0.0.1:8080/
              path: /
              authorization:
                resourceAttributes:
                  namespace: d8-{{ .Chart.Name }}
                  apiGroup: apps
                  apiVersion: v1
                  resource: deployments
                  subresource: prometheus-metrics
                  name: operator
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_only_logs" . | nindent 12 }}
{{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "helm_lib_container_kube_rbac_proxy_resources" . | nindent 12 }}
{{- end }}
